package br.com.amadeus.android.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.com.amadeus.android.entities.Course;
import br.com.amadeus.android.entities.Forum;
import br.com.amadeus.android.entities.Resource;
import br.com.amadeus.android.util.Util;

public class ForumDAO {
	
	public static String[] columns = new String[] {ForumDAO.ID, ForumDAO.NAME, ForumDAO.DESCRIPTION, ForumDAO.CREATION_DATE,
		ForumDAO.AMADEUS_ID};
	
	public static final String ID = "_id";
	public static final String NAME = "name";
	public static final String DESCRIPTION = "description";
	public static final String CREATION_DATE = "creation_date";
	public static final String AMADEUS_ID = "amadeus_id";
	
	private SQLiteDatabase db;
	
	private static ForumDAO instance = new ForumDAO();
	
	public static ForumDAO getInstance(Context ctx){
		if (instance.db == null || !instance.db.isOpen()) {
			instance.db = new DBHelper(ctx).getWritableDatabase();
		}
		return instance;
	}
	
	private ForumDAO(){
		
	}
	
	public long save(Forum forum){
		long id = forum.getId();
		if (id == 0) {
			return insert(forum);
		}else{
			return update(forum);
		}
	}

	private long insert(Forum forum) {
		long id_forum = -1;
		
		db.beginTransaction();
		try{
			SimpleDateFormat dtFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");
			ContentValues values = new ContentValues();
			values.put(ForumDAO.NAME, forum.getName());
			values.put(ForumDAO.DESCRIPTION, forum.getDescription());
			values.put(ForumDAO.CREATION_DATE, dtFormat.format(forum.getCreationDate()));
			values.put(ForumDAO.AMADEUS_ID, forum.getAmadeus_id());
			
			id_forum = db.insert(DBHelper.TABLE_FORUM, null, values);
			
			if (id_forum != -1) {
				db.setTransactionSuccessful();
			}
		}finally{
			db.endTransaction();
		}
		
		return id_forum;
	}

	private long update(Forum forum) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public void delete(int id){
		db.beginTransaction();
		try{
			db.delete(DBHelper.TABLE_FORUM, ForumDAO.ID + "=?", new String[]{String.valueOf(id)});
			db.setTransactionSuccessful();
		}finally{
			db.endTransaction();
		}
	}
		
	public Forum getForum(int forum_id){
		Cursor c = db.query(DBHelper.TABLE_FORUM, null, ForumDAO.ID + "=" + forum_id, null, null, null, null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			return fillForum(c);
		}
		c.close();
		return null;
	}
	
	protected static Forum fillForum(Cursor c){
		
		int id_id = c.getColumnIndex(ForumDAO.ID);
		int id_name = c.getColumnIndex(ForumDAO.NAME);
		int id_description = c.getColumnIndex(ForumDAO.DESCRIPTION);
		int id_creation_date = c.getColumnIndex(ForumDAO.CREATION_DATE);
		int id_amadeus_id = c.getColumnIndex(ForumDAO.AMADEUS_ID);
		
		int id = c.getInt(id_id);
		String name = c.getString(id_name);
		String description = c.getString(id_description);
		Date creationDate = Util.getDate(c.getString(id_creation_date), Util.FORMATO_DATA_BD);
		int amadeus_id = c.getInt(id_amadeus_id);
		c.close();
		
		Forum forum = new Forum(id, name, description, creationDate, amadeus_id);
		return forum;
	}
	
	public ArrayList<Forum> getForumList(){
		Cursor c = db.query(DBHelper.TABLE_FORUM, columns, null, null, null, null, null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			return fillForumList(c);
		}
		return null;
	}

	protected static  ArrayList<Forum> fillForumList(Cursor c) {
		ArrayList<Forum> forumList = new ArrayList<Forum>();
		
		do {
			int id = c.getInt(c.getColumnIndex(ForumDAO.ID));
			String name = c.getString(c.getColumnIndex(ForumDAO.NAME));
			String description = c.getString(c.getColumnIndex(ForumDAO.DESCRIPTION));
			Date creationDate = Util.getDate(c.getString(c.getColumnIndex(ForumDAO.CREATION_DATE)), Util.FORMATO_DATA_BD);
			int amadeus_id = c.getInt(c.getColumnIndex(ForumDAO.AMADEUS_ID));
			
			Forum forum = new Forum(id, name, description, creationDate, amadeus_id);
			forumList.add(forum);
			
		} while (c.moveToNext());
		
		c.close();
		return forumList;
	}

}
